package com.qfmy.web.app.service.impl.user;

import com.qfmy.common.exception.BaseException;
import com.qfmy.common.login.LoginUser;
import com.qfmy.common.login.LoginUserHolder;
import com.qfmy.common.result.ResultCodeEnum;
import com.qfmy.model.entity.user.UserFeedback;
import com.qfmy.web.app.mapper.user.UserFeedbackMapper;
import com.qfmy.web.app.service.user.UserFeedbackService;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 清风明月
 * @Package com.qfmy.web.app.service.impl.user
 * @date 2025/10/31
 * @description 用户反馈表
 */
@Service
public class UserFeedbackServiceImpl implements UserFeedbackService {

    /**
     * 意见反馈mapper
     */
    @Autowired
    private UserFeedbackMapper userFeedbackMapper;


    /**
     * 意见反馈
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void feedback(@NotNull UserFeedback userFeedback) {
        //获取当前用户id
        Long userId = getUserId();
        userFeedback.setUserId(userId);
        userFeedbackMapper.insertUserFeedback(userFeedback);
    }

    /**
     * 获取当前用户id
     * @return
     */
    @Nullable
    @Contract(pure = true)
    private Long getUserId() {
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        //判断登录用户是否为空
        if (loginUser == null) {
            throw  new BaseException(ResultCodeEnum.TOKEN_INVALID);
        }
        return loginUser.getUserId();
    }

}
